{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# This notebook assumes to be running from your FireCARES VM (eg. python manage.py shell_plus --notebook --no-browser)\n",
    "\n",
    "import sys\n",
    "import os\n",
    "import time\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "sys.path.insert(0, os.path.realpath('..'))\n",
    "import folium\n",
    "import django\n",
    "django.setup()\n",
    "from django.db import connections\n",
    "from pretty import pprint\n",
    "from firecares.firestation.models import FireDepartment, FireStation, NFIRSStatistic\n",
    "from django.db.models import Avg, Max, Min, Q\n",
    "from django.contrib.gis.geos import GEOSGeometry\n",
    "from IPython.display import display\n",
    "from firecares.utils import lenient_summation, dictfetchall\n",
    "pd.set_option(\"display.max_rows\",100)\n",
    "\n",
    "def display_geom(geom):\n",
    "    _map = folium.Map(location=[geom.centroid.y, geom.centroid.x],\n",
    "                      tiles='Stamen Toner')\n",
    "    _map.choropleth(geo_str=geom.geojson, line_weight=0, fill_opacity=0.2, fill_color='green')\n",
    "    ll = geom.extent[1::-1]\n",
    "    ur = geom.extent[3:1:-1]\n",
    "    _map.fit_bounds([ll, ur])\n",
    "\n",
    "    return _map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "q = \"\"\"select fd.id as firecares_id, name, address_line1 as hq_addr_line1, address_line2 as hq_addr_line2, city as hq_city, state_province as hq_state, postal_code as hq_postal_code, ST_X(ad.geom) as hq_x, ST_Y(ad.geom) as hq_y\n",
    "from firestation_firedepartment fd\n",
    "inner join firecares_core_address ad\n",
    "    on fd.headquarters_address_id = ad.id\n",
    "where not fd.archived;\"\"\"\n",
    "\n",
    "df = pd.read_sql_query(q, connections['default'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.to_csv('/tmp/fd_geocodes.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
